#include "include.h"	   	

#define MAX7219_DECODING				0x09	//译码方式	BCD
#define MAX7219_BRIGHTNESS				0x0a	//亮度
#define MAX7219_SCANNING_MODE			0x0b	//扫描界限	8
#define MAX7219_POWER_MODE				0x0c	//掉电模式	0	运行模式	1
#define MAX7219_DISPLAY_TEST			0x0f	//正常显示	0	显示测试	1

#define SET_BRIGHTNESS_VALUE			0x0A	//定义亮度




void MAX7219_POWER_SWITCH(unsigned char i)
{
	unsigned short count;
	
	switch(i)
	{
		case MAX7219_POWER_ON:

			MAX7219_POWER_OUT
			MAX7219_PIN_CLK_OUT
			MAX7219_PIN_CS_OUT
			MAX7219_PIN_DIN_OUT

			MAX7219_POWER_1
			MAX7219_PIN_CLK_0	
			MAX7219_PIN_CS_0	
			MAX7219_PIN_DIN_0	
			
			for(count = 0;count < 20;count ++)
			{
				Init_MAX7219();
				Delay_ms(10);
			}
			Delay_ms(300);
			break;
		case MAX7219_POWER_OFF:
		
			MAX7219_POWER_OUT
			MAX7219_PIN_CLK_OUT
			MAX7219_PIN_CS_OUT
			MAX7219_PIN_DIN_OUT

			MAX7219_POWER_0	
			MAX7219_PIN_CLK_0	
			MAX7219_PIN_CS_0	
			MAX7219_PIN_DIN_0	
			Delay_ms(300);
			break;		
	}
}

void MAX7219_RESET()
{
			MAX7219_POWER_1
			MAX7219_PIN_CLK_0	
			MAX7219_PIN_CS_0	
			MAX7219_PIN_DIN_0	
			Delay_ms(200);
	
			MAX7219_POWER_0	
			MAX7219_PIN_CLK_0	
			MAX7219_PIN_CS_0	
			MAX7219_PIN_DIN_0	
			Delay_ms(200);
}

void Sleep_MAX7219(void)
{	
	unsigned char i;

	Write_Max7219(MAX7219_POWER_MODE,0x00); 
	Write_Max7219(MAX7219_DECODING,0x00);      
	Write_Max7219(MAX7219_BRIGHTNESS,SET_BRIGHTNESS_VALUE);
	Write_Max7219(MAX7219_SCANNING_MODE,0x07);         
	Write_Max7219(MAX7219_DISPLAY_TEST,0x00);   

	//清空显示内容
	for(i = 0;i < 8;i ++)
	Write_Max7219(i + 1,0x00);  		   
}

void Init_MAX7219(void)
{	
	unsigned char i;

	//清空显示内容
	for(i = 0;i < 8;i ++)
	Write_Max7219(i + 1,0x00);  	

	Write_Max7219(MAX7219_DECODING,0x00);      
	Write_Max7219(MAX7219_BRIGHTNESS,SET_BRIGHTNESS_VALUE);
	Write_Max7219(MAX7219_SCANNING_MODE,0x07);    
	Write_Max7219(MAX7219_POWER_MODE,0x01);       
	Write_Max7219(MAX7219_DISPLAY_TEST,0x00);   
}
 

void KeepSeting_MAX7219(void)
{	
  Write_Max7219(MAX7219_DECODING,0x00);      
  Write_Max7219(MAX7219_BRIGHTNESS,SET_BRIGHTNESS_VALUE);
  Write_Max7219(MAX7219_SCANNING_MODE,0x07);    
  Write_Max7219(MAX7219_POWER_MODE,0x01);       
  Write_Max7219(MAX7219_DISPLAY_TEST,0x00);   
}


void Write_Max7219_byte(unsigned char DATA)   
{
	unsigned char i;    

	for(i = 8;i >= 1;i --)
	{		  
		MAX7219_PIN_CLK_0
		if((DATA & 0x80) == 0x80)
			MAX7219_PIN_DIN_1
		else
			MAX7219_PIN_DIN_0
		DATA = DATA << 1;
		MAX7219_PIN_CLK_1																
	}  
}

void Write_Max7219(unsigned char address1,unsigned char dat1)
{ 
	unsigned char i;  	
	
	MAX7219_PIN_CS_0

	for(i = 0;i < MAX_7219_NUMBER;i ++)
	{
		Write_Max7219_byte(address1);           
		Write_Max7219_byte(dat1);  		
	}	   

	MAX7219_PIN_CS_1
}



